package com
{ 
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import mx.binding.utils.ChangeWatcher;
	import mx.containers.Canvas;
	import mx.core.UIComponent;
 
	public class PublicFunction{
		 
		/**销毁UI函数**/
		public static function ClearContain(mc:DisplayObjectContainer):void{
			if(mc != null){
			  var len:uint = mc.numChildren;
			  for(var i:int = len-1;i>=0;i--){
				var child:* = mc.getChildAt(i);
				 
				if(child is Sprite || child is Canvas || child is UIComponent){ 
					ClearContain(child);
				}else if(child is Bitmap){
					if(Bitmap(child).bitmapData){
						var btm:BitmapData = Bitmap(child).bitmapData;
						btm.dispose();
					} 
				}else if(child is Loader){
			          Loader(child).unload();
				} 
				if(child != null){ 
				      mc.removeChild(child); 
				      child = null;   
				}
			  }
			}
		}
		
		public static function ClearContainFrom(frommc:DisplayObjectContainer,mcArr:Array):void{
		    var len:int = mcArr.length;
		    for(var i:int=0;i<len;i++){
		    	var ui:* = mcArr.pop() ;
		        if(ui != null && ui is DisplayObjectContainer){
		             ClearContain(ui);
		             if(frommc.contains(ui)){
		               frommc.removeChild(ui);
		               ui = null;
		             }
		        }
		    }  
		}

		/**设置对象字体**/
		public static function ApplyFormat(txt:TextField,size:uint,color:uint,bold:Boolean = false, font:String = "Arial",
										   autoSize:String = null,ali:String = null,dis:int=0,leading:Number=2):void{
			var tf:TextFormat = new TextFormat();
			tf.font = font;
			tf.bold = bold;
			tf.color = color;
			tf.size = size;
			tf.leading = leading;
			if(dis!=0){
			  tf.leading = dis;
			}
			if(ali != null)
			 tf.align = ali;
			txt.defaultTextFormat = tf;
			if(autoSize != null)
			 txt.autoSize = autoSize;  	
		}
		 
		public static function clearUi(ui:UIComponent):void{ 
			if(ui.numChildren > 0){
			  var l:int = ui.numChildren;
			  
			  for(var i:int=0;i<l;i++){
			  var o:* = ui.getChildAt(0);   
		         if(o != null){ 
		             ui.removeChild(o);
		             if(o is Bitmap)
		                Bitmap(o).bitmapData.dispose();
		         } 
		      }
		    }
		}
		
		public static function setPcSize(img:DisplayObject,startpoint:Point,maxw:int,maxh:int):void{
		    
		       var p:Number = img.width / img.height;
		       if(img.width > maxw){
		          img.width = maxw;
		          img.height = maxw / p;
		       }
		       if(img.height > maxh){
		          img.height = maxh;
		          img.width = maxh * p;
		       }
		       
		       img.x = startpoint.x + int((maxw - img.width)/2);
		       img.y = startpoint.y + int((maxh - img.height)/2);
		}
		
		/**加载图片**/
		public static function viewPc(url:String , ui:UIComponent , defaulturl:String = null):void{
		       clearUi(ui);
		        var loader:Loader;
		        var loaderCon:LoaderContext;
		       if(url != null && ui != null){
		         loader = new Loader(); 
		         loaderCon = new LoaderContext(true, ApplicationDomain.currentDomain );
		         loaderCon.securityDomain = null;
		         loader.contentLoaderInfo.addEventListener(Event.COMPLETE,success);
		         loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,fails);
		         loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,secerr); 
		         loader.load(new URLRequest( url ),loaderCon);
		       }
		       
		       function success(e:Event):void{
		       	     loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,success);
		             loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,fails);
		             loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,secerr);
		             var loadInfo:LoaderInfo = e.target as LoaderInfo;  
	        	     var b:Bitmap = loadInfo.content as Bitmap;
	        	     if(b != null){
	                    setPcSize(b,new Point(0,0), ui.width , ui.height); 
	                    if(ui.numChildren >= 1) {
	                       for(var c:int=ui.numChildren-1;c>=0;c--){
	                           ui.removeChildAt(c);
	                       }	                     
	                    } 
	                    ui.addChild(b);
	                 } 
		       }
		       
		       function fails(e:IOErrorEvent):void{ 
		             loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,fails);
		             if(defaulturl != null){
		                 loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,failslast);
		                 loader.load(new URLRequest( defaulturl ),loaderCon);
		             }else
		             loader = null;
		       }
		       
		       function failslast(e:IOErrorEvent):void{ 
		       	    loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,failslast); 
		            loader = null; 
		       }
		       
		       function secerr(e:SecurityErrorEvent):void{
		           loader = null;
		       } 
		}
	}
}